<template>
	<view class="page">
		<!-- 搜索条件 -->
		<view class="search-box">
			<input class="search-input" v-model="queryParams.fMaterialName" placeholder="请输入商品名称" />
			<view class="form-actions">
				<button class="btn-search" @click="getGoodsList(1)">搜索</button>
				<button class="btn-reset" @click="reset">重置</button>
			</view>
		</view>

		<!-- 商品列表（参照销售单样式） -->
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index"
				:class="{ 'selected': selectedIndexes.includes(index) }" @click="selectItem(index)">

				<view class="goods-row">
					<text class="label">商品名称：</text>
					<text class="value">{{ item.fMaterialName }}</text>
				</view>
				<view class="goods-row">
					<text class="label">规格：</text>
					<text class="value">{{ item.fSpecification }}</text>
				</view>
				<view class="goods-row">
					<text class="label">单位：</text>
					<text class="value">{{ item.fBaseUnitName }}</text>
				</view>
				<view class="goods-row">
					<text class="label">出厂价：</text>
					<text class="value">￥{{ item.fFactoryPrice }}</text>
				</view>
				<view class="goods-row">
					<text class="label">可用库存：</text>
					<text class="value">{{ item.fBaseqty }}</text>
				</view>
			</view>
		</view>

		<!-- 确认按钮 -->
		<view class="confirm-bar">
			<button class="btn-confirm" @click="confirmSelect">确认选择</button>
		</view>
	</view>
</template>

<script>
	import {
		materialList
	} from '@/api/saleOrder.js'

	export default {

		data() {
			return {
				queryParams: {
					fMaterialName: ""
				},
				goodsList: [],
				selectedIndexes: [], // 存储选中的下标,
				pageNum: 1,
				pageSize: 20,
				total: 0,
				loading: false
			}
		},
		created() {
			this.getGoodsList(1)
		},
		methods: {
			onReachBottom() {
				if (this.goodsList.length < this.total) {
					this.pageNum++
					this.getGoodsList(2)

				} else {
					uni.showToast({
						title: '没有更多了',
						icon: 'none'
					})
				}
			},

			getGoodsList(type) {

				if (this.loading) return
				this.loading = true
				uni.showLoading({
					title: '加载中...'
				}) // 显示加载弹窗
				if (type == 1) {
					this.pageNum = 1;
					this.pageSize = 20;
				}
				this.queryParams.pageNum = this.pageNum;
				this.queryParams.pageSize = this.pageSize;
				let that = this;

				materialList(this.queryParams).then(res => {
					if (res.code == 200) {
						that.total = res.total
						if (type == 1) {
							that.goodsList = [];
							that.goodsList = that.goodsList.concat(res.rows)
						} else {
							that.goodsList = that.goodsList.concat(res.rows)
						}


						that.loading = false
						uni.hideLoading() // 关闭加载弹窗
					} else {
						console.log(res.msg + "失败")
						this.loading = false
						uni.hideLoading() // 关闭加载弹窗
					}
				}).catch(() => {

					this.loading = false
					uni.hideLoading()

				})
			},
			reset() {
				this.keyword = ''
				this.selectedIndex = -1
				this.getGoodsList()
			},
			selectItem(index) {
				const i = this.selectedIndexes.indexOf(index)
				if (i > -1) {
					// 已选中 → 取消
					this.selectedIndexes.splice(i, 1)
				} else {
					// 未选中 → 添加
					this.selectedIndexes.push(index)
				}
			},
			confirmSelect() {

				if (this.selectedIndexes.length === 0) {
					uni.showToast({
						title: '请至少选择一个商品',
						icon: 'none'
					})
					return
				}
				const selectedGoods = this.selectedIndexes.map(i => this.goodsList[i])
				// 跳转传递多个数据
				uni.redirectTo({
					url: `/pages/placeAnOrder/moveTheOrder?data=${encodeURIComponent(JSON.stringify(selectedGoods))}&source=goods`
				})
			},

			formatAmount(val) {
				if (!val) return '0.00'
				return Number(val).toLocaleString('zh-CN', {
					minimumFractionDigits: 2
				})
			}
		}
	}
</script>

<style>
	.page {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	/* 搜索框样式 */
	.search-box {
		display: flex;
		flex-direction: column;
		gap: 10px;
		margin: 10px;
		background: #fff;
		padding: 20rpx;
		border-radius: 16rpx;
	}

	.search-input {
		flex: 1;
		height: 40px;
		border: 1px solid #ddd;
		border-radius: 8px;
		padding: 0 10px;
		font-size: 14px;
		background-color: #fafafa;
	}

	.form-actions {
		display: flex;
		gap: 12px;
		margin-top: 10px;
	}

	.btn-search {
		flex: 1;
		height: 40px;
		background: #4caf50;
		color: #fff;
		border-radius: 8px;
		border: none;
		font-size: 15px;
	}

	.btn-reset {
		flex: 1;
		height: 40px;
		background: #f5f5f5;
		color: #333;
		border-radius: 8px;
		border: 1px solid #ddd;
		font-size: 15px;
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin: 10px;
		padding-bottom: 120rpx;

	}

	.goods-item {
		background: #fff;
		padding: 20rpx;
		border-radius: 12rpx;
		box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.goods-item.selected {
		border: 2rpx solid #4caf50;
		position: relative;
	}

	.goods-item.selected::after {
		content: '✓';
		position: absolute;
		top: 10rpx;
		right: 20rpx;
		color: #4caf50;
		font-weight: bold;
		font-size: 24rpx;
	}

	.goods-row {
		display: flex;
		justify-content: space-between;
		padding: 6rpx 0;
		border-bottom: 1rpx dashed #eee;
	}

	.goods-row:last-child {
		border-bottom: none;
	}

	.label {
		font-weight: bold;
		color: #828080;
		width: 160rpx;
	}

	.value {
		flex: 1;
		color: #666;
		text-align: right;
	}

	/* 确认按钮栏 */
	.confirm-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background: #fff;
		padding: 20rpx;
		/* 去掉 margin，否则会被顶起来 */
		margin: 0;
		border-radius: 16rpx 16rpx 0 0;
		/* 只保留上圆角，看起来更自然 */
		box-shadow: 0 -4rpx 10rpx rgba(0, 0, 0, 0.05);
		z-index: 999;
		/* 确保浮在最上层 */
	}

	.btn-confirm {
		width: 100%;
		height: 80rpx;
		background: #007AFF;
		color: #fff;
		border-radius: 8px;
		font-size: 16px;
		font-weight: bold;
	}
</style>